package JUC;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 豆包LU
 * @date 2023/5/9 16:27
 */
public class OOMThreadPool {
    public static void main(String[] args) {
        // 创建一个可缓存的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        try {
            // 无限循环提交任务
            while (true) {
                executorService.submit(new Task());
            }
        } catch (Throwable e) {
            // 捕获异常并输出堆栈信息
            e.printStackTrace();
        } finally {
            // 关闭线程池
            executorService.shutdown();
        }
    }

    static class Task implements Runnable {
        @Override
        public void run() {
            try {
                // 休眠100毫秒，模拟任务执行
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
